/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reader;

import com.sun.org.apache.xalan.internal.xsltc.dom.BitArray;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.BitSet;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Danzz
 */
public class Filereader {
    String filename;
    Header header;
    Data data;
    
    public Filereader(String filename) {
        this.filename = filename;
    }
    
    public void doRead() {
        try {
            header = new Header();
            data = new Data(header);
            // Open the file that is the first 
            // command line parameter
            FileInputStream fstream = new FileInputStream(this.filename);
            // Get the object of DataInputStream
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strLine;
            boolean bacaData = false;
            int dataI = 0;
            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                //System.out.println(strLine);
                if(strLine.startsWith("@RELATION") || strLine.startsWith("@relation")) {
                    //header.putRelation(strLine);
                } else if(strLine.startsWith("@ATTRIBUTE") || strLine.startsWith("@attribute")) {
                    header.readAtrribute(strLine);
                } else if(strLine.startsWith("%%QUESTION") || strLine.startsWith("%%question")) {
                    header.readPertanyaan(strLine);
                } else if(strLine.startsWith("@DATA") || strLine.startsWith("@data")) {
                    bacaData = true;
                } else if (bacaData && !strLine.isEmpty()) {
                    data.putData(strLine);
                }
            }
            //Close the input stream
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error 11: " + e.getMessage());
        }
    }
    
    public Data getData()
    {
        return data;
    }
    
    public Header getHeader()
    {
        return header;
    }
    
    public static void main(String[] args) {
        Filereader fr = new Filereader("dataset\\nursery.arff");
        fr.doRead();
        System.out.println(""+fr.data.getJumlahData());
        System.out.println("Jumlah attribut "+fr.header.getNumAttribute());
        System.out.println("Max value attribut "+fr.header.getMaxJumlahValue());
        System.out.println("Max bit attribut "+fr.header.getMaxBit());
        
        String bs = fr.header.constructBitStringFromData("xx,xx,xx,xx,xx,xx,nonprob,recommended,xx");
        
        try {
            System.out.println("bs "+bs);
            System.out.println("Jumlah yang ada "+fr.data.getJumlahDataSama(bs));
            System.out.println("idx class "+fr.header.idxClass);
        } catch (Exception ex) {
            Logger.getLogger(Filereader.class.getName()).log(Level.SEVERE, null, ex);
        }
        
       
    }
}
